package com.gxa.modules.service.drivingManagementService.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gxa.common.base.RespResult;
import com.gxa.modules.entity.DrivingInfo;
import com.gxa.modules.entity.TeachArea;
import com.gxa.modules.mapper.drivingManagement.DrivingManagementMapper;
import com.gxa.modules.mapper.drivingManagement.TeachAreaMapper;
import com.gxa.modules.service.drivingManagementService.DrivingManagementService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;


@Service
@Slf4j
public class DrivingManagementServiceImpl implements DrivingManagementService {

    @Autowired
    private DrivingManagementMapper drivingManagementMapper;

    @Autowired
    private TeachAreaMapper teachAreaMapper;


    @Override
    public RespResult uploadLOGO() {//上传驾校LOGO
        return null;
    }

    @Override
    public RespResult uploadPubPicture() {//上传宣传图片
        return null;
    }

    @Override
    public RespResult showDrivingIntroduce() {//显示驾校简介

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.select("brief");
        DrivingInfo drivingInfo = drivingManagementMapper.selectOne(queryWrapper);
        try{
            return RespResult.success(drivingInfo);
        }catch (Exception e){
            e.printStackTrace();
            return RespResult.error("显示失败!");
        }
    }

    @Override
    public RespResult updateDrivingIntroduce(String brief) {//更新驾校简介
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.set("brief",brief);
        int result = drivingManagementMapper.update(null,updateWrapper);
        if(result>0){
            return RespResult.success();
        }else {
            return RespResult.error("更新失败！");
        }

    }

    @Override
    public RespResult addTeachArea(TeachArea teachArea) {//添加教学区域

        int result = teachAreaMapper.insert(teachArea);

        if(result>0){
            return RespResult.success();
        }else {
            return RespResult.error("添加失败！");
        }

    }

    @Override
    public RespResult queryAllTeachArea(int page,int limit) {//查询全部教学区域

        Page<TeachArea> teachAreaPage = new Page<>(page,limit);
        QueryWrapper<TeachArea> queryWrapper = new QueryWrapper<>();

        try{
            Page<TeachArea> teachAreaList = teachAreaMapper.selectPage(teachAreaPage,queryWrapper);
            return RespResult.success(teachAreaList);
        }catch (Exception e){
            e.printStackTrace();
            return RespResult.error("查询失败!");
        }

    }

    @Override
    public RespResult queryTeachAreaByTrainIdOrTeachAreaId(Integer trainId, Integer teachAreaId) {//根据培训机构编号和教学区域编号查询
        QueryWrapper<TeachArea> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq(StringUtils.hasText(String.valueOf(trainId)),"train_id", trainId).or()
                .eq(StringUtils.hasText(String.valueOf(teachAreaId)),"teach_area_id", teachAreaId);

        try{
            TeachArea teachArea = teachAreaMapper.selectOne(queryWrapper);
            return RespResult.success(teachArea);
        }catch (Exception e){
            e.printStackTrace();
            return RespResult.error("查询失败!");
        }
    }

    @Override
    public RespResult updateTeachArea(TeachArea teachArea) {//更新教学区域
        int result = teachAreaMapper.updateById(teachArea);

        if(result>0){
            return RespResult.success();
        }else {
            return RespResult.error("更新失败！");
        }
    }
}
